﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
//MicroDesigner
using Jc.MicroDesigner.Drawings.SpecialObjects;

namespace Jc.MicroDesigner.Drawings.Tools
{
    /// <summary>
    /// Инструмент "Область выделения"
    /// </summary>
    public sealed class RegionTool : DrawingTool
    {
        private RegionObject _Region = null;

        //предыдущее положение указателя мыши
        private int _X = 0, _Y = 0;
        bool MouseDowned = false;

        /// <summary>
        /// Событие активации
        /// </summary>
        /// <param name="toolbox"></param>
        public override void OnActivate(IToolBox toolbox)
        {
            _Region = null;
        }

        /// <summary>
        /// Нажатие клавиши мыши
        /// </summary>
        /// <param name="panel">Панель рисования</param>
        /// <param name="e">Аргументы</param>
        public override void OnMouseDown(DrawPanel panel, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _Region = new RegionObject();
                _Region.DrawOptions.Left = e.X;
                _Region.DrawOptions.Top = e.Y;
                _X = e.X;
                _Y = e.Y;
                panel.Invalidate();
                if (panel.ObjManager != null)
                {
                    _Region.Name = panel.ObjManager.NewElementName(typeof(RegionObject));
                    panel.ObjManager.Elements.Add(_Region);
                }
                MouseDowned = true;
            }
        }

        /// <summary>
        /// Событие по движению мыши
        /// </summary>
        /// <param name="panel">Панель рисования</param>
        /// <param name="e">Аргументы</param>
        public override void OnMouseMove(DrawPanel panel, MouseEventArgs e)
        {
            if ((_Region != null) && (MouseDowned))
            {
                _Region.DrawOptions.Width += (e.X - _X);
                _Region.DrawOptions.Height += (e.Y - _Y);
                _X = e.X;
                _Y = e.Y;
                _Region.DrawOptions.RefreshTemplate();
                panel.Invalidate();
            }
        }

        /// <summary>
        /// Событие прекращение нажатий
        /// </summary>
        /// <param name="panel">Панель рисования</param>
        /// <param name="e">Аргументы</param>
        public override void OnMouseUp(DrawPanel panel, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (_Region != null)
                {
                    if ((_Region.DrawOptions.Width < 4) || (_Region.DrawOptions.Height < 4))
                    {
                        if (panel.ObjManager != null)
                            panel.ObjManager.DisposeObject(_Region);
                    }
                    _Region = null;
                }
            }
        }

        /// <summary>
        /// Создать инструмент
        /// </summary>
        public RegionTool()
        {
            this._ToolCursor = Cursors.Cross;
            this._Name = "Область выделения";
        }
    }
}